package com.atyun.gaojin.notify.mailQQNotify;

import lombok.extern.slf4j.Slf4j;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
import java.util.Set;

/**
 * 发邮件的util
 * @Author: 云水瑶
 * @Date: 2025/5/1
*/
@Slf4j
public class MailQQUtil {

    //maddyjrraaisecea
    private static final String emailProtocol = "smtp"; //邮件协议

    private static final String emailSMTPHost = "smtp.qq.com"; //发件人的smtp服务器地址

    private static final String emailPort = "465"; //端口

    private static final String emailAccount = "2579895177@qq.com"; //发件人邮件地址

    private static final String emailPassword = "maddyjrraaisecea"; //发件人授权码

    /**
     * 告警邮件推送——QQ邮箱版
     * @Author: 云水瑶
     * @Date: 2025/5/1
    */
    public static boolean sendEmail(Set<String> consumerEmailSet, String title, String content) {
        if(consumerEmailSet == null || consumerEmailSet.isEmpty()) return false;

        try {
            //参数配置，用于连接邮箱服务器的参数配置
            Properties props = new Properties();
            props.setProperty("mail.transport.protocol", emailProtocol); // 使用的协议（JavaMail规范要求）
            props.setProperty("mail.smtp.host", emailSMTPHost); // 指定smtp服务器地址
            props.setProperty("mail.smtp.port", emailPort); // 指定smtp端口号
            // 使用smtp身份验证
            props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
            props.put("mail.smtp.ssl.enable", "true"); // 开启SSL
            props.put("mail.smtp.ssl.protocols", "TLSv1.2"); // 指定SSL版本
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            // 由于Properties默认不限制请求时间，可能会导致线程阻塞，所以指定请求时长
            props.setProperty("mail.smtp.connectiontimeout", "10000");// 与邮件服务器建立连接的时间限制
            props.setProperty("mail.smtp.timeout", "10000");// 邮件smtp读取的时间限制
            //根据properties创建session会话，连接邮箱服务器
            Session session = Session.getDefaultInstance(props);
            session.setDebug(true); //设置可用debug，查看更详细的log
            //创建邮件
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(emailAccount, "服务告警通知", "UTF-8"));
            int emailSize = consumerEmailSet.size();
            if(emailSize == 1) {
                //给单个人发送
                String email = consumerEmailSet.iterator().next();
                message.setRecipients(Message.RecipientType.TO, email);
            } else {
                //给多个人发送
                InternetAddress[] internetAddress = new InternetAddress[consumerEmailSet.size()];
                int index = 0;
                for (String email : consumerEmailSet) {
                    internetAddress[index ++] = new InternetAddress(email);
                }
                message.setRecipients(Message.RecipientType.TO, internetAddress);
            }
            message.setSubject(title, "UTF-8"); //设置标题
            message.setContent(content, "text/html;charset=UTF-8");
            message.setSentDate(new Date()); //立即发送
            message.saveChanges(); //设置保存
            //获取邮件传输对象
            Transport transport = session.getTransport();
            transport.connect(emailAccount, emailPassword);
            //发送邮件
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            return true;

        } catch (Exception e) {
            log.error("邮件发送异常: {}", e.getMessage());
            return false;
        }
    }

}
